package com.elitel.frame.base.controller;

import com.elitel.frame.base.service.BaseServicesService;
import com.elitel.frame.main.entity.BaseServices;
import com.elitel.frame.main.entity.dto.ServerQueryTypeAddDTO;
import com.elitel.frame.main.entity.dto.ServerQueryTypeDTO;
import com.elitel.frame.main.entity.vo.JsonResult;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/server")
public class ServerQueryController {

    @Autowired
    private BaseServicesService baseServicesService;

    @GetMapping("/getServerQueryType")
    public JsonResult getServerQueryType(ServerQueryTypeDTO queryTypeDTO) {
        List<BaseServices> service = null;
        JsonResult jsonResult = new JsonResult();
        PageHelper.startPage(queryTypeDTO.getPageNo(), queryTypeDTO.getPageSize());
        service = baseServicesService.getServerQueryType(queryTypeDTO);
        PageInfo<BaseServices> pageList = new PageInfo<>(service);
        PageResponseVo<BaseServices> res = new PageResponseVo<>();
        res.setRows(pageList.getList());
        res.setTotal(((Long) pageList.getTotal()).intValue());
        jsonResult.setSuccess(true);
        jsonResult.setData(res);
        return jsonResult;
    }

    @PostMapping("/saveServerQueryType")
    public Object saveServerQueryType(@RequestBody ServerQueryTypeAddDTO serverQueryTypeAddDTO) {
        JsonResult jsonResult = new JsonResult();
        try {
            baseServicesService.saveServerQueryType(serverQueryTypeAddDTO);
            jsonResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
    @PostMapping("/updateServerQueryType")
    public Object updateServerQueryType(@RequestBody ServerQueryTypeAddDTO serverQueryTypeAddDTO) {
        JsonResult jsonResult = new JsonResult();
        try {
            baseServicesService.updateServerQueryType(serverQueryTypeAddDTO);
            jsonResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }

    @PostMapping("/delServerQueryType")
    public Object delServerQueryType(@RequestBody ServerQueryTypeAddDTO serverQueryTypeAddDTO) {
        JsonResult jsonResult = new JsonResult();
        baseServicesService.delServerQueryType(serverQueryTypeAddDTO);
        jsonResult.setSuccess(true);
        return jsonResult;
    }
}
